package com.example.myapplication.fragment;

import android.content.Intent;

import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.myapplication.R;
import com.example.myapplication.adapter.CommunicationAdapter;
import com.example.myapplication.adapter.ICommunicationListener;
import com.example.myapplication.bean.Communication;
import com.example.myapplication.databinding.FragmentCommunicationBinding;
import com.example.myapplication.model.CommunicationModel;
import com.example.myapplication.ui.WebViewActivity;
import com.example.myapplication.widget.MyItemDecoration;

import cn.project.base.baseui.BaseNormalFragment;

public class CommunicationFragment extends BaseNormalFragment<FragmentCommunicationBinding>  implements ICommunicationListener {

    CommunicationAdapter communicationAdapter;

    private CommunicationModel communicationModel;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_communication;
    }

    @Override
    protected void initData() {
        communicationAdapter = new CommunicationAdapter(getActivity(),this);
        mBinding.rlCommunication.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.rlCommunication.addItemDecoration(new MyItemDecoration(10));
        mBinding.rlCommunication.setAdapter(communicationAdapter);

        communicationModel = new ViewModelProvider(getActivity()).get(CommunicationModel.class);

        communicationModel.getCommunicationList().observe(this,newData->{
            communicationAdapter.setNewData(newData);
            communicationAdapter.notifyDataSetChanged();
        });


    }

    @Override
    public void onCommunicationClick(Communication communication) {
        Intent intent = new Intent(getActivity(), WebViewActivity.class);
        intent.putExtra("communication",communication);
        startActivity(intent);
    }
}
